<script setup lang="ts">
import { cancelFollowShop, followShop } from "~/api/user";
import { useSocketStore } from "@/store/useSocket";
import defaultHead from "~/assets/app-mj.png";
import { useImageCache } from "~/composables/useImageCache.js";
import { computed, ref, onMounted } from "vue";

const socketStore = useSocketStore();
const { getCoverImage } = useImageCache();
const router = useRouter();
const props = defineProps({
  dataItem: {
    type: Object,
    default: () => ({}),
  },
});
const emits = defineEmits(["reload"]);

// 关注状态管理
const followStatus = ref(props.dataItem.status !== undefined ? props.dataItem.status : 1); // 从数据中获取状态，默认为已关注状态，因为在关注列表中

// 处理头像显示
const avatarSrc = computed(() => {
  if (props.dataItem.shopAvatar) {
    return getCoverImage(props.dataItem.shopAvatar);
  }
  return defaultHead;
});

// 计算显示文本
const followText = computed(() => {
  return followStatus.value === 1 ? '已关注' : '关注';
});

const toDetails = (goodsId: number, event: Event) => {
  console.log('toDetails called with goodsId:', goodsId);
  console.log('event:', event);
  console.log('props.dataItem.goodsList:', props.dataItem.goodsList);
  
  // 阻止事件冒泡
  event.stopPropagation();
  
  router.push({
    path: "/details",
    query: {
      id: goodsId,
    },
  });
};
const handleFollowClick = async () => {
  try {
    if (followStatus.value === 1) {
      // 当前已关注，执行取消关注
      await cancelFollowShop({
        shopId: props.dataItem.shopId,
      });
      followStatus.value = 0;
    } else {
      // 当前未关注，执行关注
      await followShop({
        shopId: props.dataItem.shopId,
      });
      followStatus.value = 1;
    }
    emits("reload");
  } catch (e) {
    console.log(e);
  }
};

const onlineStatus = ref(false);
onMounted(() => {
  // 获取在线状态
  if (props.dataItem.uimKey) {
    socketStore.sendMessage("userOnline", [props.dataItem.uimKey], (res) => {
      console.log(res);
      if (res.code === 200 && res.data.length) {
        onlineStatus.value = res.data[0].online === 1;
      }
    });
  }
});
</script>

<template>
  <div class="my-attention-item">
    <div class="user-info">
      <div class="user-left">
        <div class="user-head-online">
          <el-image
            :src="avatarSrc"
            fit="cover"
            style="width: 56px; height: 56px; border-radius: 50%"
          >
            <template #error>
              <img :size="56" :src="defaultHead" :title="props.dataItem.name" />
            </template>
          </el-image>
          <span :class="onlineStatus ? 'online' : 'offline'"></span>
        </div>
        <div class="user-data">
          <p class="name">{{ props.dataItem.name || "未知用户" }}</p>
          <p class="status">
            <span class="online" v-if="onlineStatus">在线 · </span>
            <!-- <span class="rate"
              >3分钟内回复率 <span>{{ props.dataItem.replyRate || 0 }}%</span></span
            > -->
          </p>
          <p class="follow-time">关注时间：{{ props.dataItem.followTime || "-" }}</p>
        </div>
      </div>
      <div class="user-right">
        <span @click="handleFollowClick">{{ followText }}</span>
      </div>
    </div>
    <div class="work-list">
      <div
        class="item"
        v-for="item of props.dataItem.goodsList"
        :key="item.goodsId"
        @click="toDetails(item.goodsId, $event)"
      >
        <img :src="item.coverPic" alt="" />
        <h4>{{ item.goodsName }}</h4>
        <p><span>¥</span>{{ item.goodsAmount }}</p>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.my-attention-item {
  width: 1104px;
  background: #ffffff;
  border-radius: 16px 16px 16px 16px;
  border: 1px solid rgba(25, 30, 40, 0.08);
  padding: 24px 24px 8px;
  box-sizing: border-box;
  margin-bottom: 16px;
}
.user-info {
  background: rgba(25, 30, 40, 0.02);
  border-radius: 12px 12px 12px 12px;
  display: flex;
  align-items: center;
  padding: 20px 24px;
  .user-left {
    flex: 1;
    display: flex;
    align-items: center;
    .user-head-online {
      width: 56px;
      height: 56px;
      position: relative;
      margin-right: 12px;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
      span {
        position: absolute;
        right: 0;
        bottom: 0;
        width: 12px;
        height: 12px;
        border-radius: 50%;
        border: 2px solid #ffffff;
        &.online {
          background: #1dbf73;
        }
        &.offline {
          background: #ccc;
        }
      }
    }
    .user-data {
      .name {
        font-size: 20px;
        font-weight: bold;
        color: #191e28;
        margin-bottom: 6px;
      }
      .status {
        color: rgba(25, 30, 40, 0.48);
        font-size: 12px;
        .online {
          color: rgba(29, 191, 115, 1);
        }
        .rate span {
          color: rgba(25, 30, 40, 0.72);
        }
      }
    }
  }
  .user-right {
    display: flex;
    align-items: center;
    span {
      cursor: pointer;
      &:nth-child(1) {
        width: 92px;
        height: 36px;
        background: #ffffff;
        border-radius: 8px 8px 8px 8px;
        border: 1px solid rgba(25, 30, 40, 0.08);
        text-align: center;
        line-height: 36px;
        color: rgba(25, 30, 40, 0.6);
        margin-right: 12px;
      }
      &:nth-child(2) {
        width: 92px;
        height: 36px;
        background: #ffffff;
        border-radius: 8px 8px 8px 8px;
        border: 1px solid #1dbf73;
        color: #1dbf73;
        text-align: center;
        line-height: 36px;
      }
    }
  }
}
.work-list {
  display: flex;
  flex-wrap: wrap;
  margin-top: 20px;
  .item {
    width: 248px;
    flex: 0 0 248px;
    display: flex;
    flex-direction: column;
    margin-right: 20px;
    border: 1px solid rgba(25, 30, 40, 0.08);
    border-radius: 16px 16px 16px 16px;
    padding-bottom: 20px;
    margin-bottom: 14px;
    box-sizing: border-box;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
    &:nth-child(4n) {
      margin-right: 0;
    }
    img {
      width: 248px;
      height: 160px;
      margin-bottom: 12px;
      border-radius: 16px 16px 16px 16px;
    }
    h4 {
      font-size: 15px;
      color: #191e28;
      padding: 0 16px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      margin-bottom: 12px;
    }
    p {
      padding: 0 16px;
      color: #ff4f4f;
      font-size: 20px;
      font-weight: bold;
      span {
        font-size: 14px;
      }
    }
  }
}
</style>
